package com.zz.wechatshop.service.impl;

import com.zz.shop.dao.ShopOrderDao;
import com.zz.shop.domain.ShopOrderDO;
import com.zz.shop.domain.ShopOrderWithProductInfo;
import com.zz.shop.domain.ShopProductDO;
import com.zz.shop.service.ShopOrderService;
import com.zz.shop.service.ShopProductService;
import com.zz.wechatshop.domain.OrderProduct;
import com.zz.wechatshop.service.OrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service(value="wechatshop_shopOrderServiceImpl")
public class OrderServiceImpl implements OrderService {


  @Resource
  ShopProductService shopProductService;
  @Resource
  ShopOrderService shopOrderService;
  @Resource
  ShopOrderDao shopOrderDao;


  @Value("${ImageHost}")
  String imageHost;



  @Override
  public List<ShopOrderWithProductInfo> addByProductIds(String userID, String comments, List<OrderProduct> productIdList) {
    String setId = UUID.randomUUID().toString();
    for (OrderProduct tempProduct: productIdList) {
      ShopProductDO product = shopProductService.getProductDetail(tempProduct.getGoodsId());
      ShopOrderDO order = new ShopOrderDO();
      order.setId(UUID.randomUUID().toString());
      order.setOrderSetId(setId);
      order.setMount(tempProduct.getNumber());
      float price = product.getSales();
      if (price == 0) {
        price = product.getPrice();
      }
      order.setStatus(1);
      order.setPrice(price);
      order.setProductId(tempProduct.getGoodsId());
      order.setUserId(userID);
      order.setCreatedBy(userID);
      order.setUpdatedBy(userID);
      shopOrderDao.add_product_to_order(order);
    }

    return shopOrderService.getOrderListByOrderSetId(userID, setId);
  }

}
